package com.neu.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.neu.MainActivity;
import com.neu.R;
import com.neu.bean.Rotation;
import com.neu.util.DBUtil;

import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {
    //定义
    private  RequestQueue mQueue=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        //获得引用
        mQueue= Volley.newRequestQueue(this);
    }

    //按钮的点击事件方法
    public void btnClick(View view) {
        //定义一个
      //  Intent intent=new Intent(this, MainActivity.class);
        Intent intent=new Intent();
        intent.putExtra("str","Hello Main");
        intent.putExtra("myInt",1000);
        //创建一个对象
        Bundle bundle=new Bundle();
        bundle.putString("myBundle","我用bundle传值");
        //放入
        intent.putExtra("bd",bundle);
        intent.setClassName("com.neu","com.neu.MainActivity");
     //   startActivity(intent);
        startActivityForResult(intent,101);

    }

    //获得activity的返回信息
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.d("TAG", "onActivityResult: ");
        Log.d("TAG", "onActivityResult: "+data.getStringExtra("back"));
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void btnLinearClick(View view) {
        Intent intent=new Intent(this,LinearActivity.class);
        startActivity(intent);

    }

    public void btnSharePre(View view) {
//        //获得对象
//        SharedPreferences sp=getSharedPreferences("config", Context.MODE_PRIVATE);
//        //获得对象的编辑器
//        SharedPreferences.Editor editor=sp.edit();
//        //写入数据
//        editor.putString("my","Hello my");
//        //提交
//        editor.commit();
        startActivity(new Intent(this,LoginActivity.class));
    }

    //内部部文件存储
    public void btnFile(View view) {
        //存储普通文件
        File file=getFilesDir();
        //存储缓存文件
        File cachDir=getCacheDir();

        Log.d("TAG", "内部普通文件的路径 "+file.getAbsolutePath());
        Log.d("TAG", "内部缓存文件的路径 "+cachDir.getAbsolutePath());

        try {
            //默认是把文件写在file
            FileOutputStream out=openFileOutput("aa.txt",Context.MODE_PRIVATE);
            out.write("hello world".getBytes());
            out.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File cfile=new File(getCacheDir(),"b.txt");
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(cfile);
            fos.write("cach刘".getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Toast.makeText(this,"文件写入成功",Toast.LENGTH_LONG).show();

    }

    public void btnSQLLite(View view) {
//        DBUtil db=new DBUtil(this);
//        SQLiteDatabase sdb=db.getWritableDatabase();
//        String sql="insert into mytable(name) values('aa')";
//        //插入 删除更新 创建
//        sdb.execSQL(sql);
//        //取出所有数据 结果
//        Cursor cursor=sdb.rawQuery("select * from mytable",null);
//        while(cursor.moveToNext()){
//            int id=cursor.getInt(0);
//            String name=cursor.getString(1);
//            Log.d("TAG", id+"====="+name);
//        }
        startActivity(new Intent(this,StudentActivity.class));

    }

    //演示
    public void btnContentProvider(View view) {
        //获得对象
        ContentResolver cr=getContentResolver();
        Uri uri= Uri.parse("content://com.neu.provider.student/insert");
        //添加一个学生
        ContentValues values=new ContentValues();
        values.put("name","王五");
        values.put("snum",10001);

        Uri idUri= cr.insert(uri,values);
        long id= ContentUris.parseId(idUri);
        Log.d("TAG", "添加的学生Id: "+id);

    }

    public void btnReadSMS(View view) {
        ContentResolver cr=getContentResolver();
        Uri uri=Uri.parse("content://sms");
        Cursor cursor=cr.query(uri, new String[]{"address", "date", "body"}, null, null, null);
        while (cursor.moveToNext()){
            String address=cursor.getString(cursor.getColumnIndex("address"));
            long data=cursor.getLong(cursor.getColumnIndex("date"));
            String body=cursor.getString(cursor.getColumnIndex("body"));
            Log.d("TAG", address+"--"+data+"---"+body);
        }
    }

    public void btnFragment(View view) {
        startActivity(new Intent(this,FragActivity.class));
    }

    public void btnHandler(View view) {
        startActivity(new Intent(this,HdActivity.class));
    }

    public void btnListView(View view) {
        startActivity(new Intent(this,LsActivity.class));
    }


//volley的使用
    public void btnVolley(View view) {
        //1、创建一个队列对象
      //  RequestQueue mQueue= Volley.newRequestQueue(this);
        //2、创建一个jsonReqest对象
        String url="http://124.93.196.45:10001/prod-api/api/rotation/list?pageNum=1&pageSize=8&type=2";
        JsonRequest jr=new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                String json=jsonObject.toString();
                Log.d("TAG", "onResponse: "+json);
                Gson gson=new Gson();
                Rotation rotation=gson.fromJson(json,Rotation.class);
                List<Rotation.RowsBean> list=rotation.getRows();
                for(Rotation.RowsBean r:list){
                    Log.d("TAG", "advImg "+r.getAdvImg());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
            }
        });

        jr.setTag("tag");

        //是否缓存数据
       // jr.setShouldCache(false);
        //3、将请求添加到队列中
        mQueue.add(jr);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQueue.cancelAll("tag");
    }

    public void btnRecycleView(View view) {
        startActivity(new Intent(this,RecActivity.class));
    }
}